/*****************************************************************************/
/*                                                                           */
/*    Domino Operation System Driver Module                                  */
/*                                                                           */
/*    Copyright (C) 2007 Laszlo Arvai                                        */
/*                                                                           */
/*    ------------------------------------------------------------------     */
/*    drvNRF24L01 -  nRF24L01 RF Communication chip driver                   */
/*****************************************************************************/

#ifndef __drvNRF24L01_h
#define __drvNRF24L01_h

#include <krnlTypes.h>

///////////////////////////////////////////////////////////////////////////////
// Constants
#define drvNRF24L01_RX_PIPE_COUNT 6
#define drvNRF24L01_MAX_PAYLOAD_LENGTH 32

///////////////////////////////////////////////////////////////////////////////
// SPI(nRF24L01) commands
#define drvNRF24L01CMD_READ_REG        			0x00  // Define read command to register
#define drvNRF24L01CMD_WRITE_REG      		 	0x20  // Define write command to register
#define drvNRF24L01CMD_RD_RX_PLOAD    		 	0x61  // Define RX payload register address
#define drvNRF24L01CMD_WR_TX_PLOAD     			0xA0  // Define TX payload register address
#define drvNRF24L01CMD_FLUSH_TX        			0xE1  // Define flush TX register command
#define drvNRF24L01CMD_FLUSH_RX        			0xE2  // Define flush RX register command
#define drvNRF24L01CMD_REUSE_TX_PL     			0xE3  // Define reuse TX payload register command
#define drvNRF24L01CMD_ACTIVATE							0x50	// Define activate command 
#define drvNRF24L01CMD_ACTIVATE_DATA				0x73	// Define second (data) byte of 'activate' command
#define drvNRF24L01CMD_R_RX_PL_WID					0x60	// Define read payload width for the top of FIFO
#define drvNRF24L01CMD_W_ACK_PAYLOAD				0xA8	// Define write payload for ACK
#define devNRF24L01CMD_W_TX_PAYLOAD_NO_ACK	0xB0	// Define disable ACK command
#define drvNRF24L01CMD_NOP									0xFF  // Define No Operation, might be used to read status register

///////////////////////////////////////////////////////////////////////////////
// SPI(nRF24L01) registers(addresses)
#define drvNRF24L01RA_CONFIG					0x00  // 'Config' register address
#define drvNRF24L01RA_EN_AA           0x01  // 'Enable Auto Acknowledgment' register address
#define drvNRF24L01RA_EN_RXADDR       0x02  // 'Enabled RX addresses' register address
#define drvNRF24L01RA_SETUP_AW        0x03  // 'Setup address width' register address
#define drvNRF24L01RA_SETUP_RETR      0x04  // 'Setup Auto. Retrans' register address
#define drvNRF24L01RA_RF_CH           0x05  // 'RF channel' register address
#define drvNRF24L01RA_RF_SETUP        0x06  // 'RF setup' register address
#define drvNRF24L01RA_STATUS          0x07  // 'Status' register address
#define drvNRF24L01RA_OBSERVE_TX      0x08  // 'Observe TX' register address
#define drvNRF24L01RA_CD              0x09  // 'Carrier Detect' register address
#define drvNRF24L01RA_RX_ADDR_P0      0x0A  // 'RX address pipe0' register address
#define drvNRF24L01RA_RX_ADDR_P1      0x0B  // 'RX address pipe1' register address
#define drvNRF24L01RA_RX_ADDR_P2      0x0C  // 'RX address pipe2' register address
#define drvNRF24L01RA_RX_ADDR_P3      0x0D  // 'RX address pipe3' register address
#define drvNRF24L01RA_RX_ADDR_P4      0x0E  // 'RX address pipe4' register address
#define drvNRF24L01RA_RX_ADDR_P5      0x0F  // 'RX address pipe5' register address
#define drvNRF24L01RA_TX_ADDR         0x10  // 'TX address' register address
#define drvNRF24L01RA_RX_PW_P0        0x11  // 'RX payload width, pipe0' register address
#define drvNRF24L01RA_RX_PW_P1        0x12  // 'RX payload width, pipe1' register address
#define drvNRF24L01RA_RX_PW_P2        0x13  // 'RX payload width, pipe2' register address
#define drvNRF24L01RA_RX_PW_P3        0x14  // 'RX payload width, pipe3' register address
#define drvNRF24L01RA_RX_PW_P4        0x15  // 'RX payload width, pipe4' register address
#define drvNRF24L01RA_RX_PW_P5        0x16  // 'RX payload width, pipe5' register address
#define drvNRF24L01RA_FIFO_STATUS     0x17  // 'FIFO Status Register' register address
#define drvNRF24L01RA_DYNPD						0x1C	// 'Enable dynamic payload length' register address
#define drvNRF24L01RA_FEATURE					0x1D	// 'Feature' register address

///////////////////////////////////////////////////////////////////////////////
// Bit definitions
///////////////////////////////////////////////////////////////////////////////

// CONFIG register bit definitions
#define drvNRF24L01CR_RESERVED				0x80
#define	drvNRF24L01CR_MASK_RX_DR			0x40
#define	drvNRF24L01CR_MASK_TX_DS			0x20
#define	drvNRF24L01CR_MASK_MAX_RT			0x10
#define	drvNRF24L01CR_EN_CRC					0x08
#define	drvNRF24L01CR_CRCO						0x04
#define	drvNRF24L01CR_PWR_UP					0x02
#define	drvNRF24L01CR_PRIM_RX					0x01

// EN_AA register bit definitions
#define drvNRF24L01EAA_RESERVED				0xC0
#define drvNRF24L01EAA_ALL						0x3F
#define drvNRF24L01EAA_P5							0x20
#define drvNRF24L01EAA_P4							0x10
#define drvNRF24L01EAA_P3							0x08
#define drvNRF24L01EAA_P2							0x04
#define drvNRF24L01EAA_P1							0x02
#define drvNRF24L01EAA_P0							0x01
#define drvNRF24L01EAA_NONE						0x00

// EN_RXADDR register bit definitions
#define drvNRF24L01RXA_RESERVED				0xC0
#define drvNRF24L01RXA_ERX_ALL				0x3F
#define drvNRF24L01RXA_ERX_P5					0x20
#define drvNRF24L01RXA_ERX_P4					0x10
#define drvNRF24L01RXA_ERX_P3					0x08
#define drvNRF24L01RXA_ERX_P2					0x04
#define drvNRF24L01RXA_ERX_P1					0x02
#define drvNRF24L01RXA_ERX_P0					0x01
#define drvNRF24L01RXA_ERX_NONE				0x00

// SETUP_AW register bit definitions
#define drvNRF24L01SAW_RESERVED				0xFC
#define drvNRF24L01SAW_MASK						0x03
#define drvNRF24L01SAW_5BYTES					0x03
#define drvNRF24L01SAW_4BYTES					0x02
#define drvNRF24L01SAW_3BYTES					0x01
#define drvNRF24L01SAW_ILLEGAL				0x00

// SETUP_RETR register bit definitions
#define drvNRF24L01SAR_ARD						0xF0
#define drvNRF24L01SAR_ARD_4000				0xF0
#define drvNRF24L01SAR_ARD_3750				0xE0
#define drvNRF24L01SAR_ARD_3500				0xD0
#define drvNRF24L01SAR_ARD_3250				0xC0
#define drvNRF24L01SAR_ARD_3000				0xB0
#define drvNRF24L01SAR_ARD_2750				0xA0
#define drvNRF24L01SAR_ARD_2500				0x90
#define drvNRF24L01SAR_ARD_2250				0x80
#define drvNRF24L01SAR_ARD_2000				0x70
#define drvNRF24L01SAR_ARD_1750				0x60
#define drvNRF24L01SAR_ARD_1500				0x50
#define drvNRF24L01SAR_ARD_1250				0x40
#define drvNRF24L01SAR_ARD_1000				0x30
#define drvNRF24L01SAR_ARD_750				0x20
#define drvNRF24L01SAR_ARD_500				0x10
#define drvNRF24L01SAR_ARD_250				0x00
#define drvNRF24L01SAR_ARC						0x0F
#define drvNRF24L01SAR_ARC_15					0x0F
#define drvNRF24L01SAR_ARC_14					0x0E
#define drvNRF24L01SAR_ARC_13					0x0D
#define drvNRF24L01SAR_ARC_12					0x0C
#define drvNRF24L01SAR_ARC_11					0x0B
#define drvNRF24L01SAR_ARC_10					0x0A
#define drvNRF24L01SAR_ARC_9					0x09
#define drvNRF24L01SAR_ARC_8					0x08
#define drvNRF24L01SAR_ARC_7					0x07
#define drvNRF24L01SAR_ARC_6					0x06
#define drvNRF24L01SAR_ARC_5					0x05
#define drvNRF24L01SAR_ARC_4					0x04
#define drvNRF24L01SAR_ARC_3					0x03
#define drvNRF24L01SAR_ARC_2					0x02
#define drvNRF24L01SAR_ARC_1					0x01
#define drvNRF24L01SAR_ARC_0					0x00

// RF_CH register bit definitions
#define drvNRF24L01RFCH_RESERVED			0x80

// RF_SETUP register bit definitions
#define drvNRF24L01RFS_RESERVED				0xE0
#define drvNRF24L01SAR_PLL_LOCK				0x10
#define drvNRF24L01SAR_RF_DR					0x08
#define drvNRF24L01SAR_RF_PWR					0x06
#define drvNRF24L01SAR_RF_PWR_0				0x06
#define drvNRF24L01SAR_RF_PWR_6				0x04
#define drvNRF24L01SAR_RF_PWR_12			0x02
#define drvNRF24L01SAR_RF_PWR_18			0x00
#define drvNRF24L01SAR_LNA_HCURR			0x01

// STATUS register bitwise definitions
#define drvNRF24L01S_RESERVED										0x80
#define drvNRF24L01S_RX_DR											0x40
#define drvNRF24L01S_TX_DS											0x20
#define drvNRF24L01S_MAX_RT											0x10
#define drvNRF24L01S_RX_P_NO										0x0E
#define drvNRF24L01S_RX_P_NO_RX_FIFO_NOT_EMPTY	0x0E
#define drvNRF24L01S_RX_P_NO_UNUSED							0x0C
#define drvNRF24L01S_RX_P_NO_5									0x0A
#define drvNRF24L01S_RX_P_NO_4									0x08
#define drvNRF24L01S_RX_P_NO_3									0x06
#define drvNRF24L01S_RX_P_NO_2									0x04
#define drvNRF24L01S_RX_P_NO_1									0x02
#define drvNRF24L01S_RX_P_NO_0									0x00
#define drvNRF24L01S_TX_FULL										0x01

// DYNPD register bitwise definitions
#define drvNRF24L01DPL_RESERVED									0xC0
#define drvNRF24L01DPL_ALL											0x3F
#define drvNRF24L01DPL_P5												0x20
#define drvNRF24L01DPL_P4												0x10
#define drvNRF24L01DPL_P3												0x08
#define drvNRF24L01DPL_P2												0x04
#define drvNRF24L01DPL_P1												0x02
#define drvNRF24L01DPL_P0												0x01

// FEATURE register bitwise definitions
#define drvNRF24L01F_RESERVED										0xF8
#define drvNRF24L01F_EN_DPL											0x04
#define drvNRF24L01F_EN_ACK_PAY									0x02
#define drvNRF24L01F_EN_DYN_ACK									0x01


///////////////////////////////////////////////////////////////////////////////
// Function prototypes

// kernel level functions
void comRFLinkInit(void);

// high level driver functions
dosByte drv24L01NRFGetChannel(void);
void drv24L01NRFSetChannel(dosByte in_channel);

// low level functions
dosByte drvNRF24L01WriteRegister( dosByte in_address, dosByte in_data );
dosByte drvNRF24L01ReadRegister( dosByte in_address );

dosByte drvNRF24L01ReadRegisters( dosByte in_command, dosByte* in_buffer, dosByte in_buffer_length);
dosByte drvNRF24L01WriteRegisters( dosByte in_command, dosByte* in_buffer, dosByte in_buffer_length);

dosByte drvNRF24L01ReadStatus(void);

// address functions
void drvNRF24L01SetTXAddress(dosByte* in_address, dosByte in_address_length );
void drvNRF24L01SetRXAddress(dosByte in_pipe, dosByte* in_address, dosByte in_address_length );

// transmit functions
dosBool drvNRF24L01IsDataSent(void);
void drvNRF24L01ClearDataSentFlag(void);
void drvNRF24L01WriteTXData( dosByte* in_buffer, dosByte in_length );
dosBool drvNRF24L01IsMaxRetry(void);
dosByte drv24L01FlushTXBuffer(void);
void drvNRF24L01WriteACKData( dosByte in_pipe, dosByte* in_buffer, dosByte in_length );

// receive functions
void drvNRF24L01SetRxMode(void);
dosBool drvNRF24L01IsDataReceived(void);
dosByte drvNRF24L01GetRXDataLength(void);
dosByte drvNRF24L01GetRXPipeNumber(void);
dosByte drvNRF24L01GetRXDataLengthOnPipe(dosByte in_pipe);
dosBool drvNRF24L01SetRXDataLength(dosByte in_pipe, dosByte in_length);
void drvNRF24L01ReadRXData(dosByte* out_buffer, dosByte in_length );


#endif
